import Vue from 'vue'
import Router from 'vue-router'
import Session from '@/helper/Session'

Vue.use(Router)

const routes = [
  {
    path: '/customer/logins',
    component: resolve => require(['../pages/customer/Login.vue'], resolve)
  },
  {
    path: '/customer/login',
    component: resolve => require(['../pages/customer/LoginId.vue'], resolve)
  },
  {
    path: '/customer',
    component: resolve => require(['../pages/customer/Main.vue'], resolve),
    children:[
      {
        path: '/customer/topmenu',
        component: resolve => require(['../pages/customer/TopMenu.vue'], resolve),
        meta: { requireAuth: true }
      },
      {
        path: '/customer/bulletinBoardList',
        component: resolve => require(['../pages/customer/BulletinBoard.vue'], resolve),
        meta: { requireAuth: true }
      },
      {
        path: '/customer/bulletinBoardDetail',
        component: resolve => require(['../pages/customer/BulletinBoardDetail.vue'], resolve),
        meta: { requireAuth: true }
      },
      {
        path: '/customer/bill',
        component: resolve => require(['../pages/customer/Bill.vue'], resolve),
        meta: { requireAuth: true }
      },
      {
        path: '/customer/reservation',
        component: resolve => require(['../pages/customer/Reservation.vue'], resolve),
        meta: { requireAuth: true }
      }
    ]
  },

  {
    path: '/manage/login',
    component: resolve => require(['../pages/manage/Login.vue'], resolve)
  },
  {
    path: '/manage',
    component: resolve => require(['../pages/manage/Main.vue'], resolve),
    children:[
      {
        path: '/manage/topmenu',
        component: resolve => require(['../pages/manage/TopMenu.vue'], resolve),
        meta: { requireAuth: true }
      },
      {
        path: '/manage/schedule',
        component: resolve => require(['../pages/manage/Schedule.vue'], resolve),
        meta: { requireAuth: true }
      },
      {
        path: '/manage/bonthlyBoard',
        component: resolve => require(['../pages/manage/MonthlyBoard.vue'], resolve),
        meta: { requireAuth: true }
      },
      {
        path: '/manage/bonthly',
        component: resolve => require(['../pages/manage/Monthly.vue'], resolve),
        meta: { requireAuth: true }
      },
      {
        path: '/manage/topmenu',
        component: resolve => require(['../pages/manage/TopMenu.vue'], resolve),
        meta: { requireAuth: true }
      },
      {
        path: '/manage/topmenu',
        component: resolve => require(['../pages/manage/TopMenu.vue'], resolve),
        meta: { requireAuth: true }
      },

    ]
  },
	//默认首页
	{ path: '*', redirect: '/customer/login' }
];

const router = new Router({
	routes: routes
});

router.beforeEach((to, from, next) => {
  let toPath = to.path;
	if (to.matched.some(r => r.meta.requireAuth)) {
		if (Session.getApiAccessToken()) {
      if (toPath == '/customer/') {
        next('/customer/topmenu');
        return;
      } else if (toPath == '/manage/') {
        next('/manage/topmenu');
        return;
      }
			next();
		} else {
			let roleMode = Session.getRoleMode() || 'customer';
			if (roleMode == 'customer') {
				next('/customer/login');
			} else if (roleMode == 'manage') {
				next('/manage/login');
			}
		}
	} else {
		next();
	}
})

export default router;